package com.tgky.ybzb.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tgky.ybzb.config.GetYml;
import com.tgky.ybzb.entity.FileInfo;
import com.tgky.ybzb.enums.MxTypeEnum;
import com.tgky.ybzb.service.FileInfoService;
import com.tgky.ybzb.utils.CommonUtil;
import com.tgky.ybzb.utils.TimeConvert;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import com.tgky.ybzb.entity.Jcmx;
import com.tgky.ybzb.service.IJcmxService;
import com.tgky.ybzb.common.IdParam;
import com.tgky.ybzb.common.PageParam;
import com.tgky.ybzb.common.Result;
import com.tgky.ybzb.utils.MPUtil;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 决策模型表 前端控制器
 *
 * @author XYP
 * @since 2024-07-22
 */
@RestController
@AllArgsConstructor
@Slf4j
@Api(value = "决策模型表", tags = "决策模型表接口")
@RequestMapping("jcmx")
public class JcmxController {
    @Resource
    private IJcmxService jcmxService;

    @Resource
    private FileInfoService fileInfoService;
    @Resource
    private GetYml getYml;

    @PostMapping("/uploadMxFile")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "上传模型文件")
    public Result uploadMxFile(MultipartFile file){
        String path=getYml.getSavePath();
        String[] today= TimeConvert.convertDateTimeToStr(TimeConvert.getCurrentDateTime()).split(" ");
        path=path+today[0]+"/"+ UUID.randomUUID()+"/";
        String savePath=CommonUtil.uploadMxFile(file,path);
        return Result.success(savePath);
    }

    /**
     * 调用 决策模型
     */
    @PostMapping("/invokeJcmx")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "调用决策模型", notes = "传入jcmx")
    public Result invokeJcmx(@RequestBody Jcmx jcmx) {
        return Result.success(jcmxService.invokeJcmx(jcmx));
    }


    /**
     * 评估 决策模型
     */
    @PostMapping("/assessJcmx")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "评估决策模型", notes = "传入jcmx")
    public Result assessJcmx(@RequestBody Jcmx jcmx) {

        return Result.success();
    }


    /**
     * 开始训练 决策模型
     */
    @PostMapping("/startTrainingJcmx")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "开始训练 决策模型", notes = "传入jcmx")
    public Result startTraining(@RequestBody Jcmx jcmx) {

        return Result.success();
    }


    /**
     * 结束训练 决策模型
     */
    @PostMapping("/endTrainingJcmx")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "结束训练 决策模型", notes = "传入jcmx")
    public Result endTraining(@RequestBody Jcmx jcmx) {

        return Result.success();
    }


    /**
     * 训练模型保存 决策模型
     */
    @PostMapping("/saveTrainJcmx")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "训练模型保存 决策模型", notes = "传入jcmx")
    public Result saveTrainJcmx(@RequestBody Jcmx jcmx) {

        return Result.success();
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "新增", notes = "传入jcmx")
    public Result save(@RequestBody Jcmx jcmx) {
        if (jcmx == null) {
            return Result.error("参数异常");
        }

        return Result.status(jcmxService.saveFilePathById(jcmx));
    }

    /**
     * 修改 决策模型表
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "修改", notes = "传入jcmx")
    public Result update(@RequestBody Jcmx jcmx) {
        if (jcmx == null) {
            return Result.error("参数异常");
        }

        return Result.status(jcmxService.updateJcmxData(jcmx));
    }

    /**
     * 删除 决策模型表
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "删除", notes = "传入ids")
    public Result remove(@RequestBody IdParam<Long> idParam) {
        if (idParam == null) {
            return Result.error("参数异常！");
        }
        for (Long id:idParam.getIds()){
            Map<String,Object> map=new HashMap<>();
            map.put("type",MxTypeEnum.JCMX.toString());
            map.put("table_id",id);
            fileInfoService.removeByMap(map);
        }

        return Result.status(jcmxService.removeByIds(idParam.getIds()));
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "详情", notes = "传入id")
    public Result<Jcmx> detail(Long id) {
        if (id == null) {
            return Result.error("参数异常");
        }
        Jcmx detail = jcmxService.getById(id);
        return Result.successData(detail);
    }

    /**
     * 查询 决策模型表
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "查询", notes = "传入jcmx")
    public Result list(PageParam pageParam, Jcmx jcmx) {
        List<Jcmx> list = jcmxService.list(jcmxService.getQueryWrapper(pageParam, jcmx));
        return Result.successData(list);
    }

    /**
     * 分页 决策模型表
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "分页", notes = "传入jcmx")
    public Result<IPage> page(PageParam pageParam, Jcmx jcmx) {
        IPage<Jcmx> pages = jcmxService.page(MPUtil.getPage(pageParam),jcmxService.getQueryWrapper(null, jcmx));
        pages.getRecords().stream().forEach(item->{
            QueryWrapper<FileInfo> fileInfoQueryWrapper=new QueryWrapper<>();
            fileInfoQueryWrapper.lambda().eq(FileInfo::getTableId,item.getMxID());
            fileInfoQueryWrapper.lambda().eq(FileInfo::getType, MxTypeEnum.JCMX.toString());
            FileInfo fileInfo=fileInfoService.getOne(fileInfoQueryWrapper);
            item.setFileInfo(fileInfo);
        });
        return Result.successData(pages);
    }

}
